
// 随机token后续要加个时间唯一标识,防止重复
function randomToken() {
    let date = new Date().toLocaleString('zh-CN').split(/[\/,:,\ ]/).join("");
    return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Number(date).toString(36);
}


// 统一时间格式转换 YYYY-MM-DD hh:mm:ss
/**
 * param:  "day" | "time" 保留到年|月|日，或者 时|分|秒(默认)
 */
function unioformDateTransform(date, param = "time"){
    let strDate = new Date(date).toLocaleString('zh-CN')
    let days = strDate.split(" ")[0]
    let time = strDate.split(" ")[1]
    let year = days.split("/")[0]
    let month = days.split("/")[1]
    let day = days.split("/")[2]

    if(Number(month)<10){
        month = '0'+ month
    }
    if(Number(day)<10){
        day = '0'+day
    }
    days =  year+'-'+month+'-'+day

    switch(param){
        case "day":
            return days
        case "time":
            return days + " " + time
    }
}


// 主键自增封装 简单的自增ID
function autoIncreamentKey(list = [],id = 'id'){
   return  list?.[0]?.[id] ? (Number(list[0][id]) + 1).toString() : '1' // 简单的自增ID
}

// 年-月-日的正则校验
const dateRegex = /([0-9]{4})-(0[1-9]|1[0|1|2])-(0[1-9]|[1|2][0-9]|3[0|1])/



export { 
    randomToken,
    unioformDateTransform,
    autoIncreamentKey,
    dateRegex
};